package mysql

import (
	"gorm.io/gorm"
	"short-video-dev/videorpc/videorpc"
)

type CommentInfo struct {
	gorm.Model
	VideoId        int    `gorm:"type:int(11);not null;comment:'视频ID'"`
	UserId         int    `gorm:"type:int(11);not null;comment:'用户ID'"`
	CommentContent string `gorm:"type:varchar(500);not null;comment:'评论内容'"`
	IsAnonymous    int    `gorm:"type:int(11);not null;default:0;comment:'是否匿名'"`
	CommentStatus  int    `gorm:"type:int(11);not null;default:0;comment:'评论状态'"`
	ReplyId        int    `gorm:"type:int(11);not null;comment:'回复ID'"`
}

func (i *CommentInfo) CommentAdd(db *gorm.DB) error {
	return db.Debug().Create(i).Error
}

func (i *CommentInfo) FindCommentById(db *gorm.DB, in *videorpc.CommentDelRequest) error {
	return db.Debug().Where("comment_infos.id=?", in.CommentId).First(i).Error
}

func (i *CommentInfo) DelComment(db *gorm.DB, in *videorpc.CommentDelRequest) error {
	return db.Debug().Where("comment_infos.id=? and comment_infos.user_id=? and comment_infos.video_id=?",
		in.CommentId, in.UserId, in.VideoId).Delete(i).Error
}
func CommentList(db *gorm.DB, in *videorpc.CommentListRequest) ([]*videorpc.CommentList, error) {
	var list []*videorpc.CommentList
	err := db.Debug().Model(&CommentInfo{}).Select("comment_infos.id as comment_id",
		"comment_infos.comment_content", "user_infos.nike_name", "user_infos.avatar_link as head_image").
		Joins("LEFT JOIN user_infos ON user_infos.id = comment_infos.user_id").
		Where("comment_infos.video_id=?", in.VideoId).Scopes(Paginate(int(in.Page), int(in.Size))).
		Order("comment_infos.created_at desc").Find(&list).Error
	return list, err
}
